package com.ks.fission.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ks.core.constant.Const;
import com.ks.core.constant.DatePanel;
import com.ks.core.dto.SearchCriteriaBase;
import com.ks.core.util.ToolUtil;
import com.ks.fission.controller.dto.BFissionTaskRequest;
import com.ks.fission.entity.BFissionTask;
import com.ks.fission.dao.BFissionTaskMapper;
import com.ks.fission.service.IBFissionTaskService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.ks.fission.service._ServiceBase;
import org.apache.ibatis.session.RowBounds;
import org.springframework.stereotype.Service;
import com.ks.core.base.OperationContext;
import com.ks.core.dto.OperateBase;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * <p>
 * 裂变-租户添加的分享裂变任务 服务实现类
 * </p>
 *
 * @author kingdee123
 * @since 2020-12-21
 */
@Service
public class BFissionTaskServiceImpl extends _ServiceBase<BFissionTaskMapper, BFissionTask> implements IBFissionTaskService {

    @Resource
    private BFissionTaskMapper bFissionTaskMapper;

    @Override
    public Map<String, Object> getBFissionTaskAdminCount(OperationContext context) {
        Map<String, Object> map = new HashMap<>();
        EntityWrapper<BFissionTask> wrapper = new EntityWrapper<>();
        BFissionTask fissionTask = new BFissionTask();
        fissionTask.setDelMark(Const.DEL_FLAG_NORMAL);
        fissionTask.setSState("1");
        wrapper.setEntity(fissionTask);
        Integer integer = bFissionTaskMapper.selectCount(wrapper);
        map.put("CardName", "分享营销数");
        map.put("DataCount", "1");
        List list = new ArrayList();
        DatePanel panel = new DatePanel();
        panel.setDataTime(new Date());
        panel.setDataValue(integer);
        panel.setValueType("STR");
        list.add(panel);
        map.put("date", list);
        return map;
    }

    @Override
    public Map<String, Object> getBFissionTaskUserCount(OperationContext context) {
        Map<String, Object> map = new HashMap<>();
        EntityWrapper<BFissionTask> wrapper = new EntityWrapper<>();
        BFissionTask fissionTask = new BFissionTask();
        fissionTask.setDelMark(Const.DEL_FLAG_NORMAL);
        fissionTask.setSState("1");
        fissionTask.setTenantID(context.getTenantId().intValue());
        wrapper.setEntity(fissionTask);
        List<BFissionTask> bFissionTasks = bFissionTaskMapper.selectList(wrapper);
        map.put("CardName", "分享裂变");
        map.put("DataCount", "3");
        List<DatePanel> list = new ArrayList();
        DatePanel panel = new DatePanel();
        panel.setDataName("广告数");
        panel.setDataTime(new Date());
        panel.setDataValue(bFissionTasks.size());
        panel.setValueType("STR");
        list.add(panel);
        map.put("date", list);
        int iReceives = 0;
        int iShares = 0;
        if (ToolUtil.isNotEmpty(bFissionTasks)) {
            for (BFissionTask bFissionTask : bFissionTasks) {
                iReceives += bFissionTask.getIReceives();
                iShares += bFissionTask.getIShares();
            }
        }
        DatePanel panel1 = new DatePanel();
        panel1.setDataName("会员分享数");
        panel1.setDataTime(new Date());
        panel1.setDataValue(iReceives);
        panel1.setValueType("STR");
        list.add(panel1);
        DatePanel panel2 = new DatePanel();
        panel2.setDataName("有效分享数");
        panel2.setDataTime(new Date());
        panel2.setDataValue(iShares);
        panel2.setValueType("STR");
        list.add(panel2);
        return map;
    }

    @Override
    public int insertBFissionTask(OperationContext operationContext, BFissionTask bFissionTask) {
        return bFissionTaskMapper.insert(bFissionTask);
    }

    @Override
    public int deleteBFissionTask(OperationContext operationContext, OperateBase operateBase) {
        int userId = operationContext.getUserId().intValue();
        ArrayList<String> stringArrayList = operateBase.getIdList();
        ArrayList<Integer> integerArrayList = new ArrayList<>();
        for (String s : stringArrayList) {
            integerArrayList.add(Integer.parseInt(s));
        }
        List<BFissionTask> bFissionTasks = bFissionTaskMapper.selectBatchIds(integerArrayList);
        List<Integer> status = new ArrayList<>();
        for (BFissionTask bFissionTask : bFissionTasks) {
            bFissionTask.setUpdateUserID(userId);
            bFissionTask.setDelMark(Const.DEL_FLAG_DEL);
            status.add(bFissionTaskMapper.updateById(bFissionTask));
        }
        if (status.contains(-1)) {
            return Const.DEL_FLAG_NORMAL;
        } else {
            return Const.DEL_FLAG_DEL;
        }
    }

    @Override
    public int updateBFissionTask(OperationContext operationContext, BFissionTask bFissionTask) {
        return bFissionTaskMapper.updateById(bFissionTask);
    }

    @Override
    public Map<String, Object> selectBFissionTaskList(OperationContext context, SearchCriteriaBase searchCriteriaBase) {
        ArrayList<SearchCriteriaBase.matchField> fields = searchCriteriaBase.getFields();
        EntityWrapper<BFissionTask> wrapper = new EntityWrapper<>();
        BFissionTask bFissionTask = new BFissionTask();
        if (ToolUtil.isNotEmpty(fields)) {
            for (SearchCriteriaBase.matchField field : fields) {
                if ("taskName".equals(field.getName())) {
                    if (ToolUtil.isNotEmpty(field.getValue())) {
                        wrapper.like("iTaskName", field.getValue());
                    }
                }
                if ("tenantID".equals(field.getName())) {
                    if (ToolUtil.isNotEmpty(field.getValue())) {
                        bFissionTask.setTenantID(Integer.valueOf(field.getValue()));
                    }
                }
                if ("state".equals(field.getName())) {
                    if (ToolUtil.isNotEmpty(field.getValue())) {
                        bFissionTask.setSState(field.getValue());
                    }
                }
            }
        }
        if (context.getTenantId().intValue() > 0) {
            bFissionTask.setTenantID(context.getTenantId().intValue());
        }
        bFissionTask.setDelMark(Const.DEL_FLAG_NORMAL);
        wrapper.setEntity(bFissionTask);
        if (ToolUtil.isNotEmpty(searchCriteriaBase.getStartTime()) && ToolUtil.isNotEmpty(searchCriteriaBase.getEndTime())) {
            wrapper.and("DATE_FORMAT( createTime, '%Y-%m-%d' ) >=" + "'" + searchCriteriaBase.getStartTime() + "'");
            wrapper.and("DATE_FORMAT( createTime, '%Y-%m-%d' ) <=  " + "'" + searchCriteriaBase.getEndTime() + "'");
        }
        Page limit = new Page(searchCriteriaBase.getPageIndex(), searchCriteriaBase.getLimit());
        List<BFissionTask> bFissionTasks = bFissionTaskMapper.selectPage(limit, wrapper);
        Map<String, Object> resultMap = this.isResultMap(bFissionTasks, searchCriteriaBase);
        Integer count = bFissionTaskMapper.selectCount(wrapper);
        resultMap.put("count", count);
        return resultMap;
    }

    @Override
    public int issueBFissionTask(OperationContext operationContext, OperateBase operateBase) {
        Integer count = 0;
        ArrayList<String> idList = operateBase.getIdList();
        for (String id : idList) {
            BFissionTask bFissionTask = bFissionTaskMapper.selectById(id);
            bFissionTask.setSState(String.valueOf(Const.DEL_FLAG_DEL));
            bFissionTask.setUpdateUserID(operationContext.getUserId().intValue());
            int i = bFissionTaskMapper.updateById(bFissionTask);
            count += i;
        }
        return count;
    }

    @Override
    public Map<String, Object> selectGetBFissionTaskList(OperationContext context, SearchCriteriaBase searchCriteriaBase) {
        ArrayList<SearchCriteriaBase.matchField> fields = searchCriteriaBase.getFields();
        EntityWrapper<BFissionTask> wrapper = new EntityWrapper<>();
        BFissionTask bFissionTask = new BFissionTask();
        for (SearchCriteriaBase.matchField field : fields) {
            if ("tenantID".equals(field.getName())) {
                bFissionTask.setTenantID(Integer.valueOf(field.getValue()));
            }
            if ("createUserID".equals(field.getName())) {
                bFissionTask.setCreateUserID(Integer.valueOf(field.getValue()));
            }
            if ("createTime".equals(field.getName())) {
                bFissionTask.setCreateTime(new Date(field.getValue()));
            }
        }

        bFissionTask.setOrgID(context.getOrgId().intValue());
        bFissionTask.setOrgSubID(context.getOrgSubId().intValue());
        bFissionTask.setTenantID(context.getTenantId().intValue());
        wrapper.setEntity(bFissionTask);
        if (searchCriteriaBase.getStartTime() != null && searchCriteriaBase.getEndTime() != null) {
            String filterSql = "createTime> '" + searchCriteriaBase.getStartTime()
                    + "' and createTime<'" + searchCriteriaBase.getEndTime() + "'";
            wrapper.addFilter(filterSql);
        }
        RowBounds limit = new Page(searchCriteriaBase.getLimit(), searchCriteriaBase.getPageIndex());
        List<BFissionTask> bFissionTasks = bFissionTaskMapper.selectPage(limit, wrapper);
        Map<String, Object> resultMap = this.isResultMap(bFissionTasks, searchCriteriaBase);
        return resultMap;
    }

    @Override
    public int getBFissionTask(OperationContext context, BFissionTaskRequest bFissionTaskRequest) {
        BFissionTask bFissionTask = bFissionTaskMapper.selectById(bFissionTaskRequest.getId());
        int iReceives = bFissionTask.getIReceives() + 1;
        bFissionTask.setIReceives(iReceives);
        return bFissionTaskMapper.updateById(bFissionTask);
    }

    @Override
    public int isStateFissionTask(OperationContext context, OperateBase operateBase) {
        Integer count = 0;
        ArrayList<String> idList = operateBase.getIdList();
        for (String id : idList) {
            BFissionTask bFissionTask = bFissionTaskMapper.selectById(id);
            bFissionTask.setSState("2");
            bFissionTask.setUpdateUserID(context.getUserId().intValue());
            int i = bFissionTaskMapper.updateById(bFissionTask);
            count += i;
        }
        return count;
    }

    @Override
    public Map<String, Object> selectTaskList(OperationContext context, SearchCriteriaBase searchCriteriaBase) {
        EntityWrapper<BFissionTask> wrapper = new EntityWrapper<>();
        BFissionTask bFissionTask = new BFissionTask();
        bFissionTask.setDelMark(Const.DEL_FLAG_NORMAL);
        bFissionTask.setOrgID(context.getOrgId().intValue());
        bFissionTask.setOrgSubID(context.getOrgSubId().intValue());
        bFissionTask.setTenantID(context.getTenantId().intValue());
        bFissionTask.setSState(String.valueOf(Const.DEL_FLAG_DEL));
        wrapper.setEntity(bFissionTask);
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = sdf.format(date);
        String filterSql = "iTaskEndTime> '" + format
                + "' and iTaskStartTime<'" + format + "'";
        wrapper.addFilter(filterSql);
        Page limit = new Page(searchCriteriaBase.getPageIndex(), searchCriteriaBase.getLimit());
        List<BFissionTask> bFissionTasks = bFissionTaskMapper.selectPage(limit, wrapper);
        Map<String, Object> resultMap = this.isResultMap(bFissionTasks, searchCriteriaBase);
        Integer count = bFissionTaskMapper.selectCount(wrapper);
        resultMap.put("count", count);
        return resultMap;
    }

    @Override
    public int addTaskViews(OperationContext context, BFissionTaskRequest bFissionTaskRequest) {
        BFissionTask bFissionTask = bFissionTaskMapper.selectById(bFissionTaskRequest.getId());
        bFissionTask.setIViews(bFissionTask.getIViews() + 1);
        return bFissionTaskMapper.updateById(bFissionTask);
    }

    @Override
    public BFissionTask getBFissionTaskInfo(int id) {
        BFissionTask bFissionTask = new BFissionTask();
        //bFissionTask.setTenantID(operationContext.getTenantId().intValue());
        bFissionTask.setId(id);
        bFissionTask.setDelMark(Const.DEL_FLAG_NORMAL);
        BFissionTask fissionTask = bFissionTaskMapper.selectOne(bFissionTask);
        return fissionTask;
    }
}
